package main

import (
	"fmt"

	"github.com/pingcap/tidb/pkg/parser"
	"github.com/pingcap/tidb/pkg/parser/ast"
	_ "github.com/pingcap/tidb/pkg/types/parser_driver"
)

func main() {
	p := parser.New()

	sql := `create table lc.t_le_logistics_require_main (
		IS_DELETED VARCHAR(1)  COMMENT '是否删除：1-删除; 0-未删除',
		REMARK VARCHAR(1000)  COMMENT '备注',
		GROUP_ID VARCHAR(64)  COMMENT '业务单据所属集团ID',
		le_logistics_require_main_id varchar(64) PRIMARY KEY COMMENT '需求 ID',
		approver_time timestamp comment '审核时间'
	) comment '物流需求主表';`

	stmtNodes, _, err := p.Parse(sql, "", "")
	if err != nil {
		fmt.Printf("parse error: %v\n", err)
		return
	}

	for _, stmtNode := range stmtNodes {
		if createTableStmt, ok := stmtNode.(*ast.CreateTableStmt); ok {
			fmt.Printf("表名: %s\n", createTableStmt.Table.Name.String())

			// 遍历所有列和约束
			for _, col := range createTableStmt.Cols {
				fmt.Printf("列名: %s\n", col.Name.Name.String())
				// 处理列的其他属性...
			}

			// 处理表级约束
			for _, constraint := range createTableStmt.Constraints {
				fmt.Printf("约束名: %s, 类型: %v\n", constraint.Name, constraint.Tp)
				// 约束类型可以是PRIMARY KEY, FOREIGN KEY等
			}
		}
	}
}
